package org.action;

import com.opensymphony.xwork2.ActionSupport;
import org.json.simple.JSONValue;
import org.model.History;
import org.model.User;
import org.model.Wiki;
import org.service.UserService;
import org.service.WikiService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * Created by tedzy on 2015/5/23.
 * History Action
 */
public class HistoryAction extends ActionSupport{
    private WikiService wikiService;
    private UserService userService;
    private List<History> histories;
    private int wikiid;
    private int id;
    private String jsonResult;
    private String content;
    private DateFormat sdf;
    private String title;
    private Wiki thisWiki;


    public String getListById(){
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        thisWiki = wikiService.getWikiById(wikiid);
        histories = wikiService.getHistories(wikiid);
        if(histories.size() <= 0){
            jsonResult = "不存在此条目";
            content = "/wiki/Index";
            return ERROR;
        }
        title = thisWiki.getTitle();
        return SUCCESS;
    }

    public String getListByAuthor(){
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        User user = userService.getUser(id);
        title = "User:" + user.getUsername();
        histories = wikiService.getAuthorHistories(id);
        if(histories.size() <= 0){
            jsonResult = "不存在此条目";
            content = "/wiki/Index";
            return ERROR;
        }
        return SUCCESS;
    }

    public String getHistory(){
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Map<String, Object> json = new LinkedHashMap<>();
        History history = wikiService.getHistory(id);

        boolean check = true;
        if(history==null){
            json.put("result", "error");
            json.put("info", "没有这样的记录。");
            check = false;
        }
        if(check){
            json.put("result", "success");
            json.put("author", history.getAuthor());
            json.put("edit_time", sdf.format(history.getEditTime()));
            json.put("title", history.getTitle());
            json.put("content", history.getContent());
            json.put("reason", history.getReason());
        }
        jsonResult = JSONValue.toJSONString(json);
        return SUCCESS;
    }

    public WikiService getWikiService() {
        return wikiService;
    }

    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }

    public List<History> getHistories() {
        return histories;
    }

    public void setHistories(List<History> histories) {
        this.histories = histories;
    }

    public int getWikiid() {
        return wikiid;
    }

    public void setWikiid(int wikiid) {
        this.wikiid = wikiid;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public DateFormat getSdf() {
        return sdf;
    }

    public void setSdf(DateFormat sdf) {
        this.sdf = sdf;
    }

    public Wiki getThisWiki() {
        return thisWiki;
    }

    public void setThisWiki(Wiki thisWiki) {
        this.thisWiki = thisWiki;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
